<html>
<head>
    <meta charset="UTF-8">
    <title>持续部署配置</title>
    <link id="easyuiTheme" href="../../static/easyui/themes/bootstrap/easyui.css" rel="stylesheet" type="text/css"/>
    <link href="../../static/easyui/themes/icon.css" rel="stylesheet" type="text/css"/>
    <link href="../../static/css/esaas-easyui-edit.css" rel="stylesheet" type="text/css"/>
    <script src="../../static/easyui/jquery.min.js" type="text/javascript"></script>
    <script src="../../static/easyui/jquery.cookie.js" type="text/javascript"></script>
    <script src="../../static/easyui/ui-theme.js" type="text/javascript"></script>
    <script src="../../static/easyui/jquery.easyui.min.js" type="text/javascript" charset="utf-8"></script>
    <script src="../../static/easyui/locale/easyui-lang-zh_CN.js" type="text/javascript"></script>
    <script src="../../static/easyui/jquery.easyui.ext.js" type="text/javascript"></script>
    <script src="../../static/plugin/assets/jslib.util.js" type="text/javascript"></script>
    <script src="../../static/plugin/assets/jslib.jquery.ajax.js" type="text/javascript"></script>
    <script src="../../static/plugin/assets/jslib.easyui.util.js" type="text/javascript"></script>
    <script src="../../ffres/dict/all.js" type="text/javascript"></script>
    <a href="" target="_blank" id="commonLink" name="commonLink"></a>
</head>
<body class="easyui-layout" style="overflow:auto">
<div>
    <form id="appff" method="post">
        <div class="esaas-info-title">应用信息<hr /></div>
        <table class="esaas-info-edit-table">
            <tr hidden="true">
                <td width="15%" align="right">应用部署标识：</td>
                <td width="35%"><input class="easyui-textbox" type="text"
                                       name="appDeployId" data-options="required:true"
                                       style="width: 100%; height: 25px;"></input></td>
            </tr>
            <tr>
                <td width="15%" align="right">应用代码：</td>
                <td width="35%"><input class="easyui-textbox" id="appCode" type="text" buttonText="选择应用"
                                       name="appCode" data-options="required:true"
                                       style="width: 100%; height: 25px;"></input></td>
                <td width="15%" align="right">应用名称：</td>
                <td width="35%"><input class="easyui-textbox" type="text"
                                       name="appName" data-options="required:true"
                                       style="width: 100%; height: 25px;"></input></td>
            </tr>
            <tr>
                <td width="15%" align="right">应用版本：</td>
                <td width="35%"><input class="easyui-textbox" type="text"
                                       name="appVersion" data-options="required:true"
                                       style="width: 100%; height: 25px;"></input></td>
                <td width="15%" align="right">应用排列顺序：</td>
                <td width="35%"><input class="easyui-textbox" type="text"
                                       name="appOrder" data-options="required:true"
                                       style="width: 100%; height: 25px;"></input></td>
            </tr>
        </table>
    </form>
    <form id="ff" method="post">
        <div class="esaas-info-title">git信息 <hr /></div>
        <table class="esaas-info-edit-table">
            <tr>
<!--                <td width="15%" align="right">本地仓库路径：</td>-->
<!--                <td width="35%"><input class="easyui-textbox" type="text"-->
<!--                                       name="gitLocalPath" data-options="required:true"-->
<!--                                       style="width: 100%; height: 25px;"></input></td>-->
                <td width="15%" align="right">远程git地址：</td>
                <td width="35%"><input class="easyui-textbox" type="text"
                                       name="gitRemotePath" id="gitRemotePath" data-options="required:true"
                                       style="width: 100%; height: 25px;"></input></td>
                <td width="15%" align="right">分支名称：</td>
                <td width="35%"><input class="easyui-textbox" type="text" value="master"
                                       name="gitBranchName" data-options="required:true"
                                       style="width: 100%; height: 25px;"></input></td>
            </tr>
            <tr>
                <td width="15%" align="right">git用户名：</td>
                <td width="35%"><input class="easyui-textbox" type="text"
                                       name="gitUserName" data-options="required:true"
                                       style="width: 100%; height: 25px;"></input></td>
                <td width="15%" align="right">git密码：</td>
                <td width="35%"><input class="easyui-textbox" type="password"
                                       name="gitPassword" data-options="required:true"
                                       style="width: 100%; height: 25px;"></input></td>
            </tr>
        </table>
    </form>
    <form id="dd" method="post">
        <div class="esaas-info-title">maven信息 <hr /></div>
        <table class="esaas-info-edit-table">
<!--            <tr>-->
<!--                <td width="15%" align="right">maven可执行文件路径：</td>-->
<!--                <td width="35%"><input class="easyui-textbox" type="text"-->
<!--                                       name="mavenHome" data-options="required:true"-->
<!--                                       style="width: 100%; height: 25px;"></input></td>-->
<!--                <td width="15%" align="right">应用路径：</td>-->
<!--                <td width="35%"><input class="easyui-textbox" type="text"-->
<!--                                       name="appPath" id="appPath" data-options="required:true"-->
<!--                                       style="width: 100%; height: 25px;"></input></td>-->
<!--            </tr>-->
            <tr>
                <td width="15%" align="right">maven参数：</td>
                <td width="35%"><input class="easyui-textbox" type="text"
                                       name="mavenParam" data-options="required:true" value="mvn clean package -U"
                                       style="width: 100%; height: 25px;"></input></td>
<!--                <td width="15%" align="right">maven本地配置文件：</td>-->
<!--                <td width="35%"><input class="easyui-textbox" type="text"-->
<!--                                       name="mavenSettings" data-options="required:true"-->
<!--                                       style="width: 100%; height: 25px;"></input></td>-->
            </tr>
        </table>
    </form>
    <div class="esaas-info-title">部署单元信息</div>
    <table id="dg" class="easyui-datagrid" title="" style="width:100%;height:250px"
           data-options="singleSelect:true,collapsible:true"
           toolbar="#tb"
           title="Load Data"
           rownumbers="true" pagination="true" pageSize="10">
        <thead>
        <tr>
            <th data-options="field:'serverUserName',resizable:true" align="center">服务器用户名</th>
            <th data-options="field:'appArtifactLocation',resizable:true" align="center" >应用编译结果目录</th>
            <th data-options="field:'appName',resizable:true" align="center" >应用部署名称</th>
            <th data-options="field:'tomcatVersion',resizable:true" align="center" >Tomcat版本</th>
            <th data-options="field:'serverIp',resizable:true" align="center">服务器IP</th>
            <th data-options="field:'tomcatPort',resizable:true" align="center">Tomcat端口</th>
        </tr>
        </thead>
    </table>
    <form id="jj" method="post"  >
        <div class="esaas-info-title">定时调度配置信息<hr /></div>
        <table class="esaas-info-edit-table">
            <tr>
                <td width="15%" align="right">服务节点标识：</td>
                <td width="35%"><input class="easyui-textbox" type="text" buttonText="选择节点"
                                       name="agentId" id="agentId" data-options="required:true"
                                       style="width: 100%; height: 25px;"></input></td>
                <td width="15%" align="right">Cron表达式：</td>
                <td width="35%"><input class="easyui-textbox" type="text"
                                       name="cronExpression" data-options="required:true"
                                       style="width: 100%; height: 25px;"></input></td>
            </tr>
            <tr>
                <td width="15%" align="right">作业类类型：</td>
                <td width="35%"><select class="easyui-combobox"
                                        id="jobClassType" name="jobClassType"
                                        style="width: 100%; height: 25px;"
                                        data-options="required:true,valueField:'enname',textField:'cnname',data:JOB_CLASS_TYPE, onSelect: onChangeJobType">
                </select></td>
                <td width="15%" align="right">定时作业描述：</td>
                <td width="35%"><input class="easyui-textbox" type="text"
                                       name="jobDesc" data-options="required:false"
                                       style="width: 100%; height: 25px;"></input></td>

            </tr>
            <tr>
                <td width="15%" align="right">作业实现类：</td>
                <td width="35%"><input class="easyui-textbox" type="text"
                                       name="jobClass" data-options="required:true"
                                       style="width: 100%; height: 25px;"></input></td>
                <td width="15%" align="right">作业类方法：</td>
                <td width="35%"><input class="easyui-textbox" type="text"
                                       name="jobMethod" data-options="required:true"
                                       style="width: 100%; height: 25px;"></input></td>

            </tr>
            <tr>
                <td width="15%" align="right"><span id="serviceVersionLabel">服务版本：</span></td>
                <td width="35%"><input class="easyui-textbox" type="text" id="serviceVersion"
                                       name="serviceVersion" data-options="required:false"
                                       style="width: 100%; height: 25px;"></input></td>
            </tr>
            <tr>
                <td width="15%" align="right"><span id="serviceGroupLabel">服务组别：</span></td>
                <td width="35%"><input class="easyui-textbox" type="text" id="serviceGroup"
                                       name="serviceGroup" data-options="required:false"
                                       style="width: 100%; height: 25px;"></input></td>
                <td width="18%" align="right"><span id="serviceTimeoutLabel">服务超时时间(毫秒)：</span></td>
                <td width="35%"><input class="easyui-textbox" type="text" id="serviceTimeout"
                                       name="serviceTimeout" data-options="required:false"
                                       style="width: 100%; height: 25px;"></input></td>
            </tr>
            <tr>
                <td width="15%" hidden align="right">定时作业编号：</td>
                <td width="35%" hidden><input class="easyui-textbox" type="text"
                                              name="jobCode" id="jobCode" data-options="required:true"
                                              style="width: 100%; height: 25px;"></input></td>
            </tr>
            <tr>
                <td width="15%" align="right">失败重试次数：</td>
                <td width="35%"><input class="easyui-textbox" type="text"
                                       name="againTime" data-options="required:true"
                                       style="width: 100%; height: 25px;"></input></td>
                <td width="15%" align="right">重试间隔秒数：</td>
                <td width="35%"><input class="easyui-textbox" type="text"
                                       name="retrySecond" data-options="required:false"
                                       style="width: 100%; height: 25px;"></input></td>
            </tr>
            <tr hidden>
                <td width="15%" align="right"><span id="subsCodeLabel">所属系统：</span></td>
                <td width="35%"><input class="easyui-textbox" type="text"
                                       name="subsCode" id="subsCode" data-options="required:false,disabled: true"
                                       style="width: 100%; height: 25px;"></input></td>
            </tr>
            <tr>
                <td width="15%" align="right">备注：</td>
                <td width="85%" colspan="3"><input class="easyui-textbox" type="text"
                                                   name="remark" data-options="required:false, multiline: true"
                                                   style="width: 100%; height: 50px;"></input></td>
            </tr>
        </table>
    </form>
</div>
<div class="esaas-info-buttons">
    <a href="javascript:void(0)" class="easyui-linkbutton " iconCls="icon-ok" onclick="submitForm()" style="width:90px">&nbsp;保&nbsp;存&nbsp;</a>
    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel" onclick="closeForm()" style="width:90px">&nbsp;关&nbsp;闭&nbsp;</a>
</div>
<div id="tb">
    <a href="javascript:doAdd()" class="easyui-linkbutton" data-options="iconCls:'icon-edit',plain:true">添加部署配置</a>
    <a href="javascript:doUpdate()" class="easyui-linkbutton" data-options="iconCls:'icon-edit',plain:true">修改</a>
    <a href="javascript:doDelete()" class="easyui-linkbutton" data-options="iconCls:'icon-edit',plain:true">删除</a>
</div>
<script>
    var autoData= new Array();
    var bpmNodeIdEsc,bpmNodeIdGit,bpmNodeIdMaven,bpmNodeIdSS;
    let bpmBaseInfoData,bpmNodeIdEscData;
    let appData;
    var bpmId ;
    var subsCode = $.getUrlParam("subsCode");

    //1--没有定时任务信息
    //2--存在定时任务信息但是没有配置节点
    //3--存在定时任务信息且存在配置节点
    var flag=1;
    $(function() {
        addTitle("持续部署配置");
        $("#appCode").textbox({
            onClickButton:function (){
                JLEUtil.createModalWinCustomSize('', 'popSysAppsInfoChoice.html?subsCode=' + subsCode,0.8,0.8);
            }
        });
        $("#agentId").textbox({
            onClickButton:function (){
                var subsCode = $.getUrlParam("subsCode");
                JLEUtil.createModalWinCustomSize('', 'popSAgentInfoChoice.html',0.8,0.8);
            }
        });

        onChangeJobType(null);
        var subsCode = $.getUrlParam("subsCode");
        $('#subsCode').textbox('setValue',subsCode);
    });

    //应用选择回调
    function onChooseApp(data){
        appData = data;
        bpmId =data.appId;
        if (data){
            $('#appff').form('load', data)
            $('#jobCode').textbox("setValue",data.appId)
            $.restfulGet('../../api/bpm/base/info/'+data.appId,null,function (data){
                if (data.rows){
                    getBpmBase();
                    getGitNode()
                    getMavenNode();
                    getSSHNode();
                }
            })
        }
    }

    //执行节点选择返回
    function onChooseAgentId(data){
        if (data){
            $("#agentId").textbox('setValue', data.agentId);
        }else {
            $.messager.alert('提示', '执行节点返回为空！', 'info');
        }
    }
    function getBpmBase(){
        //0.获取最新数据
        $.restfulGet('../../api/bpm/base/info/'+bpmId, null, function(data) {
            bpmBaseInfoData = data.rows;
            bpmBaseInfoData.bpmName = appData.appName;
            bpmBaseInfoData.latestState = 'Y';
            bpmBaseInfoData.bpmState = '1';
            bpmBaseInfoData.bpmVersion = '0.0.0'
        });
    }
    //1.获取git插件参数
    function  getGitNode(){
        var jsonData = {
            bpmId:bpmId,
            bpmNodeCode:'1095'
        };
        $.restfulPostForm('../../api/bpm/node/infos', jsonData, function(data) {
            var returnJsonData  = jQuery.parseJSON(data.rows[0].bpmNodeParam);
            bpmNodeIdGit = data.rows[0].bpmNodeId;
            if (returnJsonData!=null){
                $('#ff').form('load', {
                    gitRemotePath : returnJsonData.gitRemotePath,
                    gitUserName : returnJsonData.gitUserName,
                    gitPassword : returnJsonData.gitPassword
                });
            }else {
                $('#ff').form('load', {
                    gitRemotePath : '',
                    gitUserName : '',
                    gitPassword : ''
                });
            }
        });
    }

    //2.获取maven插件参数
    function getMavenNode(){
        var jsonData = {
            bpmId:bpmId,
            bpmNodeCode:'1071'
        };
        $.restfulPostForm('../../api/bpm/node/infos', jsonData, function(data) {
            var returnJsonData  = jQuery.parseJSON(data.rows[0].bpmNodeParam);
            bpmNodeIdMaven = data.rows[0].bpmNodeId;
            if (returnJsonData!=null){
                $('#dd').form('load', {
                    mavenParam : returnJsonData.mavenParam,
                });
            }else {
                $('#dd').form('load', {
                    mavenParam : 'mvn clean package -U',
                });
            }
        });
    }

    //3.获取服务器节点参数
    function getSSHNode(){
        var jsonData = {
            bpmId:bpmId,
            bpmNodeCode:'1072'
        };
        autoData = new Array();
        $.restfulPostForm('../../api/bpm/node/infos', jsonData, function(data) {
            if (data.rows.length>0){
                bpmNodeIdEsc = data.rows[0].bpmNodeId;
                var isNull = jQuery.parseJSON(data.rows[0].bpmNodeParam);
                if (isNull!=null){
                    for (let i = 0; i < data.rows.length; i++) {
                        var nodeData = jQuery.parseJSON(data.rows[i].bpmNodeParam);
                        nodeData.bpmNodeId = data.rows[i].bpmNodeId;
                        autoData.push(nodeData);
                    }
                }
                $('#dg').datagrid('loadData',autoData);
            }
        });
    }

    function getSSrvsCron(){
        //4.获取定时配置信息
        var jobCode = bpmId;
        $.restfulGet('../../api/s/srvs/cron/conf/' + jobCode,null,function(data){
            $('#jj').form('load', {
                jobCode : data.rows.jobCode,
                jobDesc : data.rows.jobDesc,
                jobClassType : data.rows.jobClassType,
                jobClass : data.rows.jobClass,
                jobMethod : data.rows.jobMethod,
                serviceVersion : data.rows.serviceVersion,
                serviceGroup : data.rows.serviceGroup,
                serviceTimeout : data.rows.serviceTimeout,
                cronExpression : data.rows.cronExpression,
                againTime : data.rows.againTime,
                retrySecond : data.rows.retrySecond,
                agentId : data.rows.agentId,
                subsCode:data.rows.subsCode,
                remark : data.rows.remark
            });
            onChangeJobType({enname: data.rows.jobClassType})
        },function (data){
            $('#jj').css('display','none');
        });
    }

    function doAdd(){
        var subsCode = $.getUrlParam("subsCode");
        JLEUtil.createModalWinCustomSize('选择服务器', 'popSysEscInfoChoice.html?subsCode=' + subsCode+'&bpmId='+bpmId,0.8,0.8);
    }
    function doUpdate(){
        var row = $('#dg').datagrid('getSelected');
        if (row){
            JLEUtil.createModalWin('修改', 'SSHNodeUpdate.html?' + 'bpmNodeId=' + row.bpmNodeId);
        } else {
            $.messager.alert('提示','请选择一条记录！','info');
        }
    }

    function doDelete(){
        var row = $('#dg').datagrid('getSelected');
        if (row){
            if (autoData.length==1){
                //3.服务器节点更新
                let jsonData={
                    bpmNodeId:row.bpmNodeId,
                    bpmNodeParam:'null',
                }
                $.restfulPut('../../api/bpm/node/info', jsonData, function(data) {
                    getSSHNode();
                });
            }else {
                var deletData = JSON.parse('{'+'"bpmNodeId":"' + row.bpmNodeId + '"'+'}')
                $.restfulDelete('../../api/bpm/node/info2',deletData,function (data){
                    getSSHNode();
                })
            }
        }else {
            $.messager.alert('提示','请选择一条记录！','info');
        }
    }

    // 服务器选择返回
    function onChooseOrgName(row) {

        var gitRemotePath = $("#gitRemotePath").textbox('getValue');
        var firstIndex = gitRemotePath.lastIndexOf("/");
        var lastIndex = gitRemotePath.lastIndexOf(".")
        var gitProjectName = gitRemotePath.substring(firstIndex,lastIndex);
            let jsonData = {
                serverUserName : row.ecsLoginUser,
                serverPwd :row.ecsLoginPwd,
                appArtifactLocation : row.localPath+gitProjectName+"/"+appData.appCode+"/target/"+appData.appCode+".war",
                appName : appData.appCode,
                tomcatVersion : "Tomcat 8.5.8",
                serverIp : row.ecsIp,
                tomcatPort : "8080",
                appId:bpmId
            }
            bpmNodeIdEscData = jsonData;
            debugger
            if (autoData.length == 0){
                //当只要一个默认ssh部署节点时，更新这个节点
                let jsonData={
                    bpmNodeId:bpmNodeIdEsc,
                    bpmNodeParam:JSON.stringify(bpmNodeIdEscData),
                }
                $.restfulPut('../../api/bpm/node/info', jsonData, function(data) {
                    getSSHNode();
                });
            }else {
                //否则新增ssh部署节点
                let newNodeData = {
                    bpmId:bpmId,
                    bpmNodeParam:JSON.stringify(jsonData),
                }
                $.restfulPostJson('../../api/bpm/node/info3',newNodeData,function (data){
                    getSSHNode();
                });
            }
        $('#dlg').dialog('close')
    }

    function onChangeJobType(data){
        if(data && (data.enname == 'hsf' || data.enname == 'dubbo')) {
            $("#serviceVersionLabel").show();
            $("#serviceVersion").textbox('show');
            $("#serviceGroupLabel").show();
            $("#serviceGroup").textbox('show');
            $("#serviceTimeoutLabel").show();
            $("#serviceTimeout").textbox('show');

            $("#serviceVersion").textbox('setRequired', true);
        } else {
            $("#serviceVersionLabel").hide();
            $("#serviceVersion").textbox('hide');
            $("#serviceGroupLabel").hide();
            $("#serviceGroup").textbox('hide');
            $("#serviceTimeoutLabel").hide();
            $("#serviceTimeout").textbox('hide');

            $("#serviceVersion").textbox('setRequired', false);
            $("#serviceVersion").textbox('setValue', '');
            $("#serviceGroup").textbox('setValue', '');
            $("#serviceTimeout").textbox('setValue', '');
        }
    }


    function submitForm() {
        let SrvsJsonData;
        if (!$('#ff').form('validate'))
            return ;
        if (!$('#dd').form('validate'))
            return ;
        if (!$('#jj').form('validate'))
            return ;
        //1.git插件参数更新
        let gitJsonData={
            bpmNodeId:bpmNodeIdGit,
            bpmNodeParam:JSON.stringify($("#ff").serializeJson()),
        }
        $.restfulPut('../../api/bpm/node/info', gitJsonData, function(data) {
        });

        //2.maven插件参数更新
        let mavenJsonData={
            bpmNodeId:bpmNodeIdMaven,
            bpmNodeParam:JSON.stringify($("#dd").serializeJson()),
        }
        $.restfulPut('../../api/bpm/node/info', mavenJsonData, function(data) {
        });
        // 5.执行流程发布
        $.ajax({
            type: 'POST',
            url: '../../api/bpm/base/publish/return/bpmId',
            data: JSON.stringify(bpmBaseInfoData),
            contentType:"application/json;charset=utf-8",
            dataType:"json",
            async:false,
            success: function(data) {
                if(""!=data.rows && data.rows!=null){
                    // //3.保存定时任务
                    SrvsJsonData = $("#jj").serializeJson();
                    SrvsJsonData.jobCode = data.rows;
                    $.restfulPostJson('../../api/s/srvs/cron/conf', SrvsJsonData, function(data) {
                        $.messager.confirm('确认','持续部署发布成功，您确认想要关闭页面吗？',function(r){
                            if (r){
                                window.parent.close();
                            }
                        });
                    });
                }
            }
        });
    }

    function closeForm() {
        $.messager.confirm('确认','您确认想要关闭页面吗？',function(r){
            if (r){
                window.parent.close();
            }
        });
    }
</script>
</body>
</html>